//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Membership
{

	public class PostingOptions : BaseMembershipPage
	{

		#region Child Controls

		protected YesNoRadioButtonList PublicMemberList;
		protected YesNoRadioButtonList MemberListAdvancedSearch;
		protected YesNoRadioButtonList optEnableUserModerationCounters;
		protected TextBox MemberListPageSize;
		protected RequiredFieldValidator MemberListPageSizeValidator;
		protected ResourceLinkButton btnSave;
		protected StatusMessage formStatus;

		#endregion

		
		override protected void OnInit(EventArgs e)
		{

			btnSave.Click += new EventHandler(Save_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.DataBind();
			}

		}

		public override void DataBind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
    
			formStatus.Visible = false;
			PublicMemberList.SelectedValue = settings.EnablePublicMemberList;
			MemberListAdvancedSearch.SelectedValue = settings.EnablePublicAdvancedMemberSearch;
			optEnableUserModerationCounters.SelectedValue = settings.EnableUserModerationCounters;
			MemberListPageSize.Text = settings.MembersPerPage.ToString();

		}
	
		private void Save_Click(Object sender, EventArgs e) 
		{

			if (!Page.IsValid)
				return;
			
			SaveSettings();

			formStatus.Visible = true;
			formStatus.Success = true;
			formStatus.Text = string.Format(ResourceManager.GetString("CP_Membership_Cache_Warning"), CSContext.Current.SiteSettings.SiteSettingsCacheWindowInMinutes).Replace("'", @"\'");
		}

		protected virtual void SaveSettings() 
		{
			SiteSettings settings = CSContext.Current.SiteSettings; 

			settings.EnablePublicMemberList = PublicMemberList.SelectedValue;
			settings.EnablePublicAdvancedMemberSearch = MemberListAdvancedSearch.SelectedValue;
			settings.EnableUserModerationCounters = optEnableUserModerationCounters.SelectedValue;

			try 
			{ 
				settings.MembersPerPage = Int32.Parse( MemberListPageSize.Text ); 
				if(Int32.Parse( MemberListPageSize.Text ) < 1)
					MemberListPageSizeValidator.IsValid = false;
			}
			catch { MemberListPageSizeValidator.IsValid = false; }

			SiteSettingsManager.Save(settings);
			
		}

	}
}
